import pygame
import sys
from bullet import Bullet
from badball import Badball
from time import sleep






def check_keydown_events(event, ai_settings, screen, myball, bullets):
   
    if event.key==pygame.K_RIGHT:
        myball.moving_right = True
    elif event.key==pygame.K_LEFT:
        myball.moving_left = True
    elif event.key==pygame.K_SPACE:
        
        #创建新炮弹并将其加入到编组bullets中
        if len(bullets) < ai_settings.bullets_allowed:
            new_bullet = Bullet(ai_settings, screen, myball)
            bullets.add( new_bullet)
    elif event.key==pygame.K_q:
        sys.exit()
        
def check_keyup_events(event, myball):
    if event.key==pygame.K_RIGHT:
        myball.moving_right = False
    elif event.key==pygame.K_LEFT:
        myball.moving_left = False
    
def check_events(ai_settings, screen, stats, play_button, myball, badballs, bullets):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event, ai_settings, screen, myball, bullets)
            
        elif event.type == pygame.KEYUP:
            check_keyup_events(event, myball)
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_x, mouse_y = pygame.mouse.get_pos()
            check_play_button(ai_settings, screen, stats, play_button, myball, badballs, bullets, mouse_x, mouse_y)

def check_play_button(ai_settings, screen, stats, play_button, myball, badballs, bullets, mouse_x, mouse_y):

    button_clicked = play_button.rect.collidepoint(mouse_x, mouse_y)

    if  button_clicked and not stats.game_active:
        pygame.mouse.set_visible(False)
         
        stats.reset_stats() 
        stats.game_active = True

        badballs.empty()
        bullets.empty()
        create_fleet(ai_settings, screen, myball, badballs)
        myball.center_ship()

                
def update_screen(ai_settings, screen, stats, myball, badballs, bullets,  play_button):
    screen.fill(ai_settings.bg_color)
    for bullet in bullets.sprites():
        bullet.draw_bullet()
    myball.blitme()
    badballs.draw(screen)
    
    if not stats.game_active:
        play_button.draw_button()
        
    pygame.display.flip()

def update_bullets(ai_settings, screen, myball, badballs, bullets):
    bullets.update()
    for bullet in bullets.copy():
        if bullet.rect.bottom <= 0:
            bullets.remove(bullet)
    #检查是否击中敌人
    collisions = pygame.sprite.groupcollide(bullets, badballs, True, True)
    if len(badballs) == 0 :
        bullets.empty()
        create_fleet(ai_settings,screen,myball,badballs)
def check_badballs_bottom(ai_settings, screen, myball, badballs, bullets):
    screen_rect = screen.get_rect()
    for badball in badballs.sprites():
        if badball.rect.bottom >= screen_rect.bottom :
            bullets.empty()
            create_fleet(ai_settings, screen, myball, badballs)


def get_number_rows(ai_settings, myball_height, badball_height):
  """计算屏幕可容纳多少行敌人"""
  available_space_y = (ai_settings.screen_height - (3 * badball_height) - myball_height)
  number_rows = int(available_space_y / (2 * badball_height))
  return number_rows



def create_badball(ai_settings, screen, badballs, badball_number, row_number):
    #creatte a badball and put it inthe first line
    badball = Badball(ai_settings,screen)
    badball_width = badball.rect.width
    badball.x = badball_width + 2 * badball_width * badball_number
    badball.rect.x = badball.x
    badball.rect.y = badball.rect.height + 2 * badball.rect.height * row_number
    badballs.add(badball)

def get_number_badballs_x(ai_settings, badball_width): 
    available_space_x = ai_settings.screen_width - 2 * badball_width
    number_badballs_x = int (available_space_x / (2 * badball_width))
    return number_badballs_x

def create_fleet(ai_settings, screen, myball, badballs) :
    #创建敌人群
    badball = Badball(ai_settings, screen)
    number_badballs_x = get_number_badballs_x(ai_settings, badball.rect.width)
    number_rows = get_number_rows(ai_settings, myball.rect.height, badball.rect.height) 
     # 第一行敌人
    for row_number in range (number_rows):
        for badball_number in range (number_badballs_x):
            create_badball(ai_settings, screen, badballs, badball_number, row_number)


    
def check_fleet_edges(ai_settings, badballs):
    #当有敌人到达边缘时采取相应措施
    for badball in badballs.sprites():
        if badball.check_edges():
            change_fleet_direction(ai_settings, badballs)
            #break

def change_fleet_direction(ai_settings, badballs):
    #将敌人下移，并改变方向
    for badball in badballs.sprites():
        badball.rect.y += ai_settings.fleet_drop_speed
    ai_settings.fleet_direction *= -1    
        
def check_badballs_bottom(ai_settings, stats, screen, myball, badballs, bullets):

    screen_rect = screen.get_rect()
    for badball in badballs.sprites():
        if badball.rect.bottom >= screen_rect.bottom:
            myball_hit(ai_settings, stats, screen, myball, badballs, bullets)
            break
    
def myball_hit(ai_settings, stats, screen, myball, badballs, bullets):

    if stats.myballs_left > 0:

        stats.myballs_left = stats.myballs_left - 1

        badballs.empty()
        bullets.empty()

        create_fleet(ai_settings, screen, myball, badballs)
        myball.center_ship()


        sleep(0.5)

    else:
        stats.game_active = False 
        pygame.mouse.set_visible(True)
         
 


def update_badballs(ai_settings, stats, screen, myball, badballs, bullets):
    
    check_fleet_edges(ai_settings, badballs)
    
    badballs.update()
    

    if pygame.sprite.spritecollideany(myball, badballs):
        print("玩个球啊！")
        myball_hit(ai_settings, stats, screen, myball, badballs, bullets)
    check_badballs_bottom(ai_settings, stats, screen, myball, badballs, bullets)








    


 


    
   
